package br.com.megaacerto.controle;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jrimum.bopepo.BancosSuportados;
import org.jrimum.bopepo.Boleto;
import org.jrimum.bopepo.view.BoletoViewer;
import org.jrimum.domkee.comum.pessoa.endereco.CEP;
import org.jrimum.domkee.comum.pessoa.endereco.Endereco;
import org.jrimum.domkee.comum.pessoa.endereco.UnidadeFederativa;
import org.jrimum.domkee.financeiro.banco.febraban.Agencia;
import org.jrimum.domkee.financeiro.banco.febraban.Carteira;
import org.jrimum.domkee.financeiro.banco.febraban.Cedente;
import org.jrimum.domkee.financeiro.banco.febraban.ContaBancaria;
import org.jrimum.domkee.financeiro.banco.febraban.NumeroDaConta;
import org.jrimum.domkee.financeiro.banco.febraban.Sacado;
import org.jrimum.domkee.financeiro.banco.febraban.SacadorAvalista;
import org.jrimum.domkee.financeiro.banco.febraban.TipoDeTitulo;
import org.jrimum.domkee.financeiro.banco.febraban.Titulo;
import org.jrimum.domkee.financeiro.banco.febraban.Titulo.Aceite;

import com.google.gson.Gson;

import br.com.megaacerto.dto.BoletoDTO;
import br.com.megaacerto.dto.UsuarioLogadoDTO;
import br.com.megaacerto.negocio.PagamentoBU;

public class BoletoControle extends HttpServlet {

	@Override
	//TODO Passa idUsuario
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

//		String idUsuario = req.getParameter("idusuario");
//		String idUsuario = "1";
		String valor = req.getParameter("valor");
		UsuarioLogadoDTO usuarioLogadoDTO = (UsuarioLogadoDTO) req.getSession().getAttribute("usuario");
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(
				"http://localhost:8080/restservice/pagamento/inserirPagamentoBoleto");

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("idUsuario", ""+usuarioLogadoDTO.getId()));
		nameValuePairs.add(new BasicNameValuePair("valor", valor));
		nameValuePairs.add(new BasicNameValuePair("tipoPagamento", "1"));

		BoletoDTO boletoDTO = null;
		
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse httpResponse = httpClient.execute(httpPost);

			BufferedReader rd = new BufferedReader(new InputStreamReader(
					httpResponse.getEntity().getContent()));
			String line = "";
			
			Gson gson = new Gson();
			while ((line = rd.readLine()) != null) {
				boletoDTO = gson.fromJson(line, BoletoDTO.class);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		PagamentoBU pagamentoBU = new PagamentoBU();
		
		Boleto boleto = pagamentoBU.gerarBoleto(boletoDTO);
		
		BoletoViewer boletoViewer = new BoletoViewer(boleto);

		resp.setContentType("application/pdf");
		
		File arquivoPdf = boletoViewer.getPdfAsFile("MeuPrimeiroBoleto.pdf");
		
		FileInputStream fileInputStream = new FileInputStream(arquivoPdf);
		OutputStream outputStream = resp.getOutputStream();
		int bytes;
		while( (bytes = fileInputStream.read()) != -1 ){
			outputStream.write(bytes);
		}

		outputStream.flush();
		outputStream.close();

	}

}
